<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改游记攻略')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-youji-edit" th:object="${tYouji}">
            <input name="yid" th:field="*{yid}" type="hidden">

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">文章封面：</label>
                <div class="col-sm-8">
                    <div class="row" style="height: auto;cursor: pointer;border:1px gainsboro solid;border-radius: 10px 10px;text-align: center" id="upload_div">
                        <img src="" alt="" >
                        <span class="vicp-hint">点击上传图片至此处</span>
                    </div>
                    <input type="file" name="file" id="upload_img" style="display: none;"/>
                    <input type="text" name="coverURL" th:field="*{coverURL}" id="coverURL" style="display: none;"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">文章标题：</label>
                <div class="col-sm-8">
                    <input name="title" th:field="*{title}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">发表时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="time" th:value="${#dates.format(tYouji.time, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">作者用户id：</label>
                <div class="col-sm-8">
                    <!--<input name="uid" th:field="*{uid}" class="form-control" type="text" required>-->

                    <select id="user" class="form-control select2-selection" name="uid">
                        <!--<option value="">&#45;&#45;请选择游记作者&#45;&#45;</option>-->
                        <option th:selected="#{#{uid} eq ${user.uid}}" th:each="user:${users}" th:value="${user.uid}" th:text="${user.nickname}"></option>
                    </select>


                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">文章内容：</label>
                <div class="col-sm-8">
                    <input id="textContent" name="text" th:field="*{text}" type="hidden">
                    <div id="editor" class="summernote"></div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "travel/youji";
        $("#form-youji-edit").validate({
            focusCleanup: true
        });

        $(function() {
            $('.summernote').summernote({
                placeholder: '请输入公告内容',
                height : 192,
                lang : 'zh-CN',
                followingToolbar: false,
                callbacks: {
                    onImageUpload: function (files) {
                        sendFile(files[0], this);
                    }
                }
            });
            var content = $("#textContent").val();
            $('#editor').summernote('code', content);

            var coverURL = $("#coverURL").val();
            $("#upload_div").html('<img src="'+coverURL+'" alt="" class="img-responsive img-rounded">' +
                '<img src="" alt="" >\n' +
                '                        <span class="vicp-hint">点击上传替换图片至此处</span>\n');
        });

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }


        function submitHandler() {
            if ($.validate.form()) {
                var sHTML = $('.summernote').summernote('code');
                $("#textContent").val(sHTML);
                $.operate.save(prefix + "/edit", $('#form-youji-edit').serialize());

            }
        }

        $("input[name='time']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });


        $("#upload_div").click(function () {//div点击触发inputfile按钮上传文件的功能
            $("#upload_img").click();
        });

        $("#upload_img").change(function () {//选中文件后,异步上传文件并获取上传文件的地址
            var data = new FormData();
            data.append("file", document.getElementById("upload_img").files[0]);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        //异步刷新图片
                        var image_URL=result.fileName
                        $("#coverURL").val(image_URL)
                        $("#upload_div").html('<img src="'+image_URL+'" alt="" class="img-responsive img-rounded">\n');
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            })
        });
    </script>
</body>
</html>
